﻿using Microsoft.CodeAnalysis;
using System.Collections.Generic;

namespace DimensionsHelper.SourceGeneration.Enumerator
{


    public class EnumConvertCondition
    {

        /// <summary>
        /// 值类型是否是带有<see cref="System.FlagsAttribute"/>的枚举类型
        /// </summary>
        public bool IsFlag { get; set; }


        /// <summary>
        /// 如果<see cref="TypeFullName"/>是字符，返回<see langword="true"/>
        /// </summary>
        public bool IsChar { get; private set; }


        /// <summary>
        /// 如果<see cref="TypeFullName"/>是字符串，返回<see langword="true"/>
        /// </summary>
        public bool IsString { get; private set; }


        private string _typeFullName;


        /// <summary>
        /// 类型名
        /// </summary>
        public string TypeFullName
        {
            get
            {
                return _typeFullName;
            }

            set
            {
                _typeFullName = value;

                if (_typeFullName == "char")
                {
                    IsChar = true;
                    IsString = false;
                }
                else if (_typeFullName == "string")
                {
                    IsChar = false;
                    IsString = true;
                }
            }
        }


        /// <summary>
        /// 构造函数参数索引
        /// </summary>
        public int ConstructorArgIndex { get; set; }


        /// <summary>
        /// 命名参数名
        /// </summary>
        public string ArgumentName { get; set; }

    }



    public abstract class EnumGenerationInfoBase
    {

        /// <summary>
        /// 指定的特性名，默认是<see langword="null"/>。
        /// </summary>
        public ITypeSymbol AttributeType { get; set; }


        /// <summary>
        /// 绑定的构造函数参数索引
        /// </summary>
        public int AttributeConstructorArgIndex { get; set; }


        /// <summary>
        /// 绑定的属性名
        /// </summary>
        public string AttributeArgumentName { get; set; }


        /// <summary>
        /// 如果查找特性的派生类，设置此属性为<see langword="true"/>。
        /// </summary>
        public bool AttributeDerivedType { get; set; }


        /// <summary>
        /// 目标类型，如果此属性是<see langword="null"/>或<see cref="string.Empty"/>，
        /// 将从枚举类型名称中截取
        /// </summary>
        public string TargetName { get; set; }


        /// <summary>
        /// 枚举类型全名
        /// </summary>
        public string EnumFullName { get; set; }


        /// <summary>
        /// 帮助类所在命名空间
        /// </summary>
        public string Namespace { get; set; }


        /// <summary>
        /// 帮助类型名
        /// </summary>
        public string ClassName { get; set; }


        /// <summary>
        /// 帮助类是否是静态类
        /// </summary>
        public bool IsStatic { get; set; }


        /// <summary>
        /// 帮助类是否带有internal限定符
        /// </summary>
        public bool IsInternal { get; set; }


        /// <summary>
        /// 如果属性类型是枚举类型，将此属性设置为<see langword="true"/>
        /// </summary>
        public bool IsTargetValueEnum { get; set; }


        /// <summary>
        /// 如果属性类型是字符串，将此属性设置为<see langword="true"/>
        /// </summary>
        public bool IsTargetValueString { get; set; }


        /// <summary>
        /// 如果属性类型是字符，将此属性设置为<see langword="true"/>
        /// </summary>
        public bool IsTargetValueChar { get; set; }


        /// <summary>
        /// 枚举转换值的有效条件，默认是<see langword="null"/>
        /// </summary>
        public EnumConvertCondition Condition { get; set; }


        /// <summary>
        /// 枚举值列表
        /// </summary>
        public List<EnumConvertValueInfo> EnumValues { get; set; }
    }


    public class EnumConvertValueInfo
    {

        /// <summary>
        /// 转换值所在的生成信息
        /// </summary>
        public EnumGenerationInfoBase GenerationInfo { get; set; }


        /// <summary>
        /// 枚举成员全名
        /// </summary>
        public string EnumMemberFullName { get; set; }


        /// <summary>
        /// 枚举转换值
        /// </summary>
        public string Value { get; set; }


        /// <summary>
        /// 进行值检测时的有效条件值，默认是<see langword="null"/>
        /// </summary>
        public string ConditionValue { get; set; }


        /// <summary>
        /// 获取<paramref name="value"/>对应的代码文本
        /// </summary>
        public static string GetValue(string value, bool isChar, bool isString)
        {
            if (value == null)
            {
                return value;
            }

            if (isChar)
            {
                if (value.Length > 1)
                {
                    return null;
                }

                return $"'{value.ToEscapedString()}'";
            }

            if (isString)
            {
                return $"\"{value.ToEscapedString()}\"";
            }

            return value;
        }


        public string GetConvertValue() => 
            GetValue(Value, 
                GenerationInfo?.IsTargetValueChar ?? false, 
                GenerationInfo?.IsTargetValueString ?? false);

        public string GetConditionValue() => 
            GetValue(ConditionValue, 
                GenerationInfo?.Condition?.IsChar ?? false, 
                GenerationInfo?.Condition?.IsString ?? false);
    }


    public class EnumStringGenerationInfo : EnumGenerationInfoBase
    {

        /// <summary>
        /// 是否忽略大小写
        /// </summary>
        public bool IgnoreCase { get; set; }


        /// <summary>
        /// 是否生成字符串 -> 枚举类型值的函数
        /// </summary>
        public bool ConvertBack { get; set; }

    }


    public class EnumAttributePropertySelectorInfo : EnumGenerationInfoBase
    {


        /// <summary>
        /// 属性类型全名
        /// </summary>
        public string PropertyTypeFullName { get; set; }


        /// <summary>
        /// 如果属性类型是值类型，将此属性设置为<see langword="true"/>
        /// </summary>
        public bool IsValueType { get; set; }


        /// <summary>
        /// 如果属性类型是可空类型，将此属性设置为<see langword="true"/>
        /// </summary>
        public bool IsNullable { get; set; }

    }

}
